package com.xander.myblog.controller;

import com.xander.myblog.bean.ResultBean;
import com.xander.myblog.entity.Topic;
import com.xander.myblog.service.TopicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author hbxz
 * @date 2018/12/20 0020
 */
@Controller
@RequestMapping("topic")
public class TopicController {
    @Autowired
    private TopicService topicService;

    @RequestMapping("addTopic")
    @ResponseBody
    public ResultBean addTopic(Topic topic) {
        topicService.insertTopic(topic);
        return new ResultBean();
    }

    @RequestMapping("delTopic")
    @ResponseBody
    public ResultBean delTopic(Long tid) {
        Topic topic = new Topic();
        topic.setTid(tid);
        topicService.deleteTopic(topic);
        return new ResultBean();
    }

    @RequestMapping("getAllTopic")
    @ResponseBody
    public ResultBean getAllTopic() {
        List<Topic> existTopicList = topicService.getExistTopic();
        return new ResultBean(existTopicList);
    }

    @RequestMapping("getTopicBytid")
    @ResponseBody
    public ResultBean getTopicBytid(Long tid) {
        Topic topic = topicService.getTopicByTid(tid);
        return new ResultBean(topic);
    }

    @RequestMapping("updateTopic")
    @ResponseBody
    public ResultBean updateTopic(Topic topic) {
        if(topic.getTid() == null){
            topicService.insertTopic(topic);
        }else{
            topicService.updateTopic(topic);
        }
        return new ResultBean();
    }

}
